import {ElMessage} from "element-plus";

/**
 * 根据当前时间生成14位字符串序列
 * @returns {string}
 */
const createId = () => {
  let date = new Date();
  return '' + date.getFullYear() + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1))
      + (date.getDate() < 10 ? '0' + (date.getDate()) : (date.getDate()))
      + (date.getHours() < 10 ? '0' + (date.getHours()) : (date.getHours()))
      + (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : (date.getMinutes()))
      + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
}

const timestempToDateString = (tempStemp) => {
  let date = new Date(parseInt(tempStemp));
  return '' + date.getFullYear() + '年' + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + '月'
      + (date.getDate() < 10 ? '0' + (date.getDate()) : (date.getDate())) + '日 '
      + (date.getHours() < 10 ? '0' + (date.getHours()) : (date.getHours())) + ':'
      + (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : (date.getMinutes()));
}

/**
 * 根据时间字符串 转换为时间
 */
const dateStringToTimeStemp = (str) => {
  let yearStr = str.substr(0, 4);
  let monthStr = str.substr(4, 2);
  let dateStr = str.substr(6, 2);
  let hourStr = str.substr(8, 2);
  let minutesStr = str.substr(10, 2);
  let millisecondsStr = str.substr(12, 3);
  let date = new Date();
  date.setFullYear(yearStr);
  date.setMonth(monthStr - 1);
  date.setDate(dateStr)
  date.setHours(hourStr)
  date.setMinutes(minutesStr)
  date.setMilliseconds(millisecondsStr)
  return date.getTime()
}

/**
 * 金额计算公式
 */
const calc = (goods) => {
  if (goods.price != "" && goods.count != "") {
    goods.price = parseInt(goods.price * 100)
    goods.count = parseInt(goods.count)
    goods.money = goods.price * goods.count;
    if (goods.transport != "") {
      goods.transport = parseInt(goods.transport * 100);
      goods.money = goods.money + goods.transport;
    }
  }
  return goods.money / 100;
}

/**
 * 根据返回的结果,显示信息
 * @param value
 */
const showMessage = (value) => {
  if (value !== 0) {
    ElMessage({message: "操作成功", type: 'success'})
  } else {
    ElMessage({message: "操作失败", type: 'error'})
  }
}

const showCustomMessage = (message, type) => {
  ElMessage({message, type})
}


export {createId, dateStringToTimeStemp, calc, showMessage, showCustomMessage, timestempToDateString}